home *** CD-ROM | disk | FTP | other *** search
/ Aminet 44 / Aminet 44 (2001)(GTI - Schatztruhe)[!][Aug 2001].iso / Aminet / dev / misc / AmigaSDLsrc.lha / amisrc / SDL_ahiaudio.c < prev    next >
C/C++ Source or Header  |  2000-08-24  |  9KB  |  346 lines

  1. /*
  2.     SDL - Simple DirectMedia Layer
  3.     Copyright (C) 1997, 1998  Sam Lantinga
  4.  
  5.     This library is free software; you can redistribute it and/or
  6.     modify it under the terms of the GNU Library General Public
  7.     License as published by the Free Software Foundation; either
  8.     version 2 of the License, or (at your option) any later version.
  9.  
  10.     This library is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.     Library General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU Library General Public
  16.     License along with this library; if not, write to the Free
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.     Sam Lantinga
  20.     5635-34 Springhouse Dr.
  21.     Pleasanton, CA 94588 (USA)
  22.     slouken@devolution.com
  23. */
  24.  
  25. #ifdef SAVE_RCSID
  26. static char rcsid =
  27.  "@(#) $Id: SDL_sysaudio.c,v 1.3.2.1 2000/06/01 02:15:22 hercules Exp $";
  28. #endif
  29.  
  30. /* Allow access to a raw mixing buffer (For IRIX 6.5 and higher) */
  31.  
  32. #include "SDL_endian.h"
  33. #include "SDL_audio.h"
  34. #include "SDL_audiomem.h"
  35. #include "SDL_audio_c.h"
  36. #include "SDL_lowaudio.h"
  37.  
  38. /* Audio driver functions */
  39. static int AHI_OpenAudio(_THIS, SDL_AudioSpec *spec);
  40. static void AHI_WaitAudio(_THIS);
  41. static void AHI_PlayAudio(_THIS);
  42. static Uint8 *AHI_GetAudioBuf(_THIS);
  43. static void AHI_CloseAudio(_THIS);
  44.  
  45. #ifndef __SASC
  46.     #define mymalloc(x) AllocVec(x,MEMF_PUBLIC) 
  47.     #define myfree FreeVec
  48. #else
  49.     #define mymalloc malloc
  50.     #define myfree free
  51. #endif
  52.  
  53. /* Audio driver bootstrap functions */
  54.  
  55. static int Audio_Available(void)
  56. {
  57.     int ok=0;
  58.     struct MsgPort *p;
  59.     struct AHIRequest *req;
  60.  
  61.     if(p=CreateMsgPort())
  62.     {
  63.         if(req=(struct AHIRequest *)CreateIORequest(p,sizeof(struct AHIRequest)))
  64.         {
  65.             req->ahir_Version=4;
  66.  
  67.             if(!OpenDevice(AHINAME,0,(struct IORequest *)req,NULL))
  68.             {
  69.                 D(bug("AHI available.\n"));
  70.                 ok=1;
  71.                 CloseDevice((struct IORequest *)req);
  72.             }
  73.             DeleteIORequest((struct IORequest *)req);
  74.         }
  75.         DeleteMsgPort(p);
  76.     }
  77.  
  78.     D(if(!ok) bug("AHI not available\n"));
  79.     return ok;
  80. }
  81.  
  82. static void Audio_DeleteDevice(SDL_AudioDevice *device)
  83. {
  84.     free(device->hidden);
  85.     free(device);
  86. }
  87.  
  88. static SDL_AudioDevice *Audio_CreateDevice(int devindex)
  89. {
  90.     SDL_AudioDevice *this;
  91.  
  92. #ifndef NO_AMIGADEBUG
  93.     D(bug("AHI created...\n"));
  94. #endif
  95.  
  96.     /* Initialize all variables that we clean on shutdown */
  97.     this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
  98.     if ( this ) {
  99.         memset(this, 0, (sizeof *this));
  100.         this->hidden = (struct SDL_PrivateAudioData *)
  101.                 malloc((sizeof *this->hidden));
  102.     }
  103.     if ( (this == NULL) || (this->hidden == NULL) ) {
  104.         SDL_OutOfMemory();
  105.         if ( this ) {
  106.             free(this);
  107.         }
  108.         return(0);
  109.     }
  110.     memset(this->hidden, 0, (sizeof *this->hidden));
  111.  
  112.     /* Set the function pointers */
  113.     this->OpenAudio = AHI_OpenAudio;
  114.     this->WaitAudio = AHI_WaitAudio;
  115.     this->PlayAudio = AHI_PlayAudio;
  116.     this->GetAudioBuf = AHI_GetAudioBuf;
  117.     this->CloseAudio = AHI_CloseAudio;
  118.  
  119.     this->free = Audio_DeleteDevice;
  120.  
  121.     return this;
  122. }
  123.  
  124. AudioBootStrap AHI_bootstrap = {
  125.     "AHI", Audio_Available, Audio_CreateDevice
  126. };
  127.  
  128.  
  129. void static AHI_WaitAudio(_THIS)
  130. {
  131.     if(!CheckIO((struct IORequest *)audio_req[current_buffer]))
  132.     {
  133.         WaitIO((struct IORequest *)audio_req[current_buffer]);
  134. //        AbortIO((struct IORequest *)audio_req[current_buffer]);
  135.     }
  136. }
  137.  
  138. static void AHI_PlayAudio(_THIS)
  139. {
  140.     if(playing>1)
  141.         WaitIO((struct IORequest *)audio_req[current_buffer]);
  142.  
  143.     /* Write the audio data out */
  144.     audio_req[current_buffer] -> ahir_Std. io_Message.mn_Node.ln_Pri = 60;
  145.     audio_req[current_buffer] -> ahir_Std. io_Data                = mixbuf[current_buffer];
  146.     audio_req[current_buffer] -> ahir_Std. io_Length              = this->hidden->size;
  147.     audio_req[current_buffer] -> ahir_Std. io_Offset              = 0;
  148.     audio_req[current_buffer] -> ahir_Std . io_Command            = CMD_WRITE;
  149.     audio_req[current_buffer] -> ahir_Frequency                   = this->hidden->freq;
  150.     audio_req[current_buffer] -> ahir_Volume                      = 0x10000;
  151.     audio_req[current_buffer] -> ahir_Type                        = this->hidden->type;
  152.     audio_req[current_buffer] -> ahir_Position                    = 0x8000;
  153.     audio_req[current_buffer] -> ahir_Link                        = (playing>0 ? audio_req[current_buffer^1] : NULL);
  154.  
  155.     SendIO((struct IORequest *)audio_req[current_buffer]);
  156.     current_buffer^=1;
  157.  
  158.     playing++;
  159. }
  160.  
  161. static Uint8 *AHI_GetAudioBuf(_THIS)
  162. {
  163.     return(mixbuf[current_buffer]);
  164. }
  165.  
  166. static void AHI_CloseAudio(_THIS)
  167. {
  168.     D(bug("Closing audio...\n"));
  169.  
  170.     playing=0;
  171.  
  172.     if(audio_req[0])
  173.     {
  174.         if(audio_req[1])
  175.         {
  176.             D(bug("Break req[1]...\n"));
  177.  
  178.             AbortIO((struct IORequest *)audio_req[1]);
  179.             WaitIO((struct IORequest *)audio_req[1]);
  180.         }
  181.  
  182.         D(bug("Break req[0]...\n"));
  183.  
  184.         AbortIO((struct IORequest *)audio_req[0]);
  185.         WaitIO((struct IORequest *)audio_req[0]);
  186.  
  187.         if(audio_req[1])
  188.         {
  189.             D(bug("Break AGAIN req[1]...\n"));
  190.             AbortIO((struct IORequest *)audio_req[1]);
  191.             WaitIO((struct IORequest *)audio_req[1]);
  192.         }
  193. // Double abort to be sure to break the dbuffering process.
  194.  
  195.         SDL_Delay(200);
  196.  
  197.         D(bug("Reqs breaked, closing device...\n"));
  198.         CloseDevice((struct IORequest *)audio_req[0]);
  199.         D(bug("Device closed, freeing memory...\n"));
  200.         myfree(audio_req[1]);
  201.         D(bug("Memory freed, deleting IOReq...\n")); 
  202.         DeleteIORequest((struct IORequest *)audio_req[0]);
  203.         audio_req[0]=audio_req[1]=NULL;
  204.     }
  205.  
  206.     D(bug("Freeing mixbuf[0]...\n"));
  207.     if ( mixbuf[0] != NULL ) {
  208.         myfree(mixbuf[0]);
  209. //        SDL_FreeAudioMem(mixbuf[0]);
  210.         mixbuf[0] = NULL;
  211.     }
  212.  
  213.     D(bug("Freeing mixbuf[1]...\n"));
  214.     if ( mixbuf[1] != NULL ) {
  215.         myfree(mixbuf[1]);
  216. //        SDL_FreeAudioMem(mixbuf[1]);
  217.         mixbuf[1] = NULL;
  218.     }
  219.  
  220.     D(bug("Freeing audio_port...\n"));
  221.  
  222.     if ( audio_port != NULL ) {
  223.         DeleteMsgPort(audio_port);
  224.         audio_port = NULL;
  225.     }
  226.     D(bug("...done!\n"));
  227. }
  228.  
  229. static int AHI_OpenAudio(_THIS, SDL_AudioSpec *spec)
  230. {    
  231. //    int width;
  232.  
  233.     D(bug("AHI opening...\n"));
  234.  
  235.     /* Determine the audio parameters from the AudioSpec */
  236.     switch ( spec->format & 0xFF ) {
  237.  
  238.         case 8: { /* Signed 8 bit audio data */
  239.             D(bug("Samples a 8 bit...\n"));
  240.             spec->format = AUDIO_S8;
  241.             this->hidden->bytespersample=1;
  242.             if(spec->channels<2)
  243.                 this->hidden->type = AHIST_M8S;
  244.             else
  245.                 this->hidden->type = AHIST_S8S;
  246.         }
  247.         break;
  248.  
  249.         case 16: { /* Signed 16 bit audio data */
  250.             D(bug("Samples a 16 bit...\n"));
  251.             spec->format = AUDIO_S16MSB;
  252.             this->hidden->bytespersample=2;
  253.             if(spec->channels<2)
  254.                 this->hidden->type = AHIST_M16S;
  255.             else
  256.                 this->hidden->type = AHIST_S16S;
  257.         }
  258.         break;
  259.  
  260.         default: {
  261.             SDL_SetError("Unsupported audio format");
  262.             return(-1);
  263.         }
  264.     }
  265.  
  266.     if(spec->channels!=1 && spec->channels!=2)
  267.     {
  268.         D(bug("Wrong channel number!\n"));
  269.         SDL_SetError("Channel number non supported");
  270.         return -1;
  271.     }
  272.  
  273.     D(bug("Before CalculateAudioSpec\n"));
  274.     /* Update the fragment size as size in bytes */
  275.     SDL_CalculateAudioSpec(spec);
  276.  
  277.     D(bug("Before CreateMsgPort\n"));
  278.  
  279.     if(!(audio_port=CreateMsgPort()))
  280.     {
  281.         SDL_SetError("Unable to create a MsgPort");
  282.         return -1;
  283.     }
  284.  
  285.     D(bug("Before CreateIORequest\n"));
  286.  
  287.     if(!(audio_req[0]=(struct AHIRequest *)CreateIORequest(audio_port,sizeof(struct AHIRequest))))
  288.     {
  289.         SDL_SetError("Unable to create an AHIRequest");
  290.         DeleteMsgPort(audio_port);
  291.         return -1;
  292.     }
  293.  
  294.     audio_req[0]->ahir_Version = 4;
  295.  
  296.     if(OpenDevice(AHINAME,0,(struct IORequest *)audio_req[0],NULL))
  297.     {
  298.         SDL_SetError("Unable to open AHI device!\n");
  299.         DeleteIORequest((struct IORequest *)audio_req[0]);
  300.         DeleteMsgPort(audio_port);
  301.         return -1;
  302.     }
  303.     
  304.     D(bug("AFTER opendevice\n"));
  305.  
  306.     /* Set output frequency and size */
  307.     this->hidden->freq = spec->freq;
  308.     this->hidden->size = spec->size;
  309.  
  310.     D(bug("Before buffer allocation\n"));
  311.  
  312.     /* Allocate mixing buffer */
  313.     mixbuf[0] = (Uint8 *)mymalloc(spec->size);
  314.     mixbuf[1] = (Uint8 *)mymalloc(spec->size);
  315.  
  316.     D(bug("Before audio_req allocation\n"));
  317.  
  318.     if(!(audio_req[1]=mymalloc(sizeof(struct AHIRequest))))
  319.     {
  320.         SDL_OutOfMemory();
  321.         return(-1);
  322.     }
  323.     
  324.     D(bug("Before audio_req memcpy\n"));
  325.  
  326.     memcpy(audio_req[1],audio_req[0],sizeof(struct AHIRequest));
  327.  
  328.     if ( mixbuf[0] == NULL || mixbuf[1] == NULL ) {
  329.         SDL_OutOfMemory();
  330.         return(-1);
  331.     }
  332.  
  333.     D(bug("Before mixbuf memset\n"));
  334.  
  335.     memset(mixbuf[0], spec->silence, spec->size);
  336.     memset(mixbuf[1], spec->silence, spec->size);
  337.  
  338.     current_buffer=0;
  339.     playing=0;
  340.  
  341.     D(bug("AHI opened: freq:%ld mixbuf:%lx/%lx buflen:%ld bits:%ld channels:%ld\n",spec->freq,mixbuf[0],mixbuf[1],spec->size,this->hidden->bytespersample*8,spec->channels));
  342.  
  343.     /* We're ready to rock and roll. :-) */
  344.     return(0);
  345. }
  346.